package com.ieheima.ai.controller;


import com.ieheima.ai.Entry.MessageVo;
import com.ieheima.ai.memory.ChatMap;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;


@RequiredArgsConstructor
@RestController
@RequestMapping("/ai/history")
public class ChatHistoryController {

     private final ChatMap chatMap;

     private final ChatMemory chatMemory;

     @GetMapping("/{type}")
     public List<String> getHistory(@PathVariable String type) {

         return chatMap.get(type);
     }


    @GetMapping("/{type}/{chatId}")
    public List<MessageVo> getMessage(@PathVariable String type, @PathVariable String chatId) {

         List<Message> messages = chatMemory.get(chatId);
        System.out.println(messages);
        List<MessageVo> r = messages.stream().map(message -> new MessageVo(message)).collect(Collectors.toList());

        return r;
    }


}
